<script lang="ts" setup>
import { getDoctorList, transferDoctor } from "../service";
import { IResponseData } from "@/@types/utils.request";

const props = defineProps({
	modelValue: {
		type: Boolean,
		default: false,
	},
	orderNo: {
		type: String,
		default: "",
	},
});

const emit = defineEmits<{
	(e: "update:modelValue", value: boolean): void;
	(e: "refresh", value: boolean): void;
}>();
const visible = computed({
	get() {
		return props.modelValue;
	},
	set(val) {
		emit("update:modelValue", !!val);
	},
});
const orderNo = computed(() => props.orderNo);

interface Ilist {
	doctorId: number;
	doctorName: string;
}

const list = ref<Ilist[]>([]);
const selectValue = ref<number | string>("");
const getFetch = async (val: string) => {
	try {
		const response: IResponseData<any> = await getDoctorList({
			pageSize: 100,
			pageNum: 1,
			doctorName: val || "",
		} as any);
		const { code, msg, data } = response;
		if (code === 0) {
			console.log(data);
			list.value = data.records;
		} else {
			ElMessage({
				message: msg,
				type: "error",
			});
		}
	} catch (error: any) {
		console.log(error);
	}
};

const formRef = ref<any>();
const onConfirmm = async () => {
	if (!(await formRef.value.validate())) return;
	try {
		const response: IResponseData<any> = await transferDoctor({ doctorId: selectValue.value, orderNo: orderNo.value });
		const { code, msg } = response;
		if (code === 0) {
			ElMessage({
				message: msg,
				type: "success",
			});
			emit("refresh", true);
			emit("update:modelValue", false);
		} else {
			ElMessage({
				message: msg,
				type: "error",
			});
		}
	} catch (error: any) {
		console.log(error);
	}
};
const onCancel = () => {
	selectValue.value = "";
	emit("update:modelValue", false);
};
</script>
<template>
	<el-dialog v-model="visible" title="临床医师转单" width="500px" @open="getFetch('')" :before-close="onCancel">
		<el-select
			v-model="selectValue"
			filterable
			remote
			remote-show-suffix
			:remote-method="getFetch"
			placeholder="请选择转单的临床医师"
			style="width: 100%"
		>
			<el-option v-for="item in list" :key="item.doctorId" :label="item.doctorName" :value="item.doctorId"> </el-option>
		</el-select>

		<template #footer>
			<span class="dialog-footer">
				<el-button @click="onCancel">取消</el-button>
				<el-button type="primary" @click="onConfirmm"> 确认 </el-button>
			</span>
		</template>
	</el-dialog>
</template>
